<?php

namespace app\admin\model\tongcheng;

use addons\tongcheng\model\BaseModel;


class CouponRecordModel extends BaseModel
{


    // 表名
    protected $name = 'tongcheng_coupon_record';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'expire_time_text',
        'status_text'
    ];


    public function getTypeList()
    {
        return ['VOUCHER' => __('VOUCHER'), 'FULL' => __('FULL')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list  = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getExpireTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expire_time']) ? $data['expire_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setExpireTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function coupon()
    {
        return $this->belongsTo('Coupon', 'coupon_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function zone()
    {
        return $this->belongsTo('Zone', 'zone_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 回退优惠券记录
     * @param DeliveryOrderModel $order
     * @return void
     */
    public static function rollbackRecord($order)
    {
        if (empty($order['fees']['coupon_record_id'])) {
            return;
        }
        if (!$couponRecord = CouponRecordModel::get($order['fees']['coupon_record_id'])) {
            return;
        }
        if ($couponRecord['expire_time'] < time()) {
            return;
        }

        // 回退优惠券记录使用状态
        $couponRecord->status = 0;
        $couponRecord->save();

        // 回退优惠券使用数量
        CouponModel::where('id', $couponRecord['coupon_id'])->dec('used_count')->update();

        // 回退订单优惠金额
        $order->discounts -= $couponRecord['money'];
        $order->save();

        // 清空订单优惠券记录
        $order->fees->coupon_id        = 0;
        $order->fees->coupon_record_id = 0;
        $order->fees->coupon_discount  = 0;
        $order->fees->save();
    }
}
